<?php

// 完善项目运营数据
// PUT project.data.complete

namespace app\api\resource\project\data;
use think\Db;
use think\Loader;
use think\Session;

class Complete
{
    // 方法
    public $_method = ['PUT'];
    // 前置
    public $_pre    = ['login'];
    // 描述
    public $_description = '完善项目运营数据';
    // 参数
    public $_param  = [
        'project_id' => '项目主键',
        'industry'   => '行业',
        'DATA'       => '详见sql',
    ];
    // 返回
    public $_return = [
        201 => '完善成功',
        400 => '项目不存在',
        422 => '请求参数不合法',
        500 => '服务器错误',
    ];

    public function run(&$request)
    {
        $params = $request->put();

        $validate = Loader::validate('ProjectData');
        if ( !$validate->scene('complete')->check($params) ) {
            $errMsg = $validate->getError();
            return [422,$errMsg];
        }

        $memberId  = Session::get('member.id');
        
        $project = Db::name('project')->where('id',$params['project_id'])->where('member_id',$memberId)->find();
        if ( is_null($project) ) return [400,'项目不存在'];

        $num = Db::name('project_data')->where('project_id',$params['project_id'])->update($params);

        return ($num >= 0) ? [201,$params] : 500;
    }
}